package dao

import (
	"ai/internal/db/models"

	"gorm.io/gorm"
)

type ActivityDAO struct {
	db *gorm.DB
}

func NewActivityDAO(db *gorm.DB) *ActivityDAO {
	return &ActivityDAO{db: db}
}

// CreateActivity adds a new activity to the database
func (dao *ActivityDAO) CreateActivity(activity *models.Activity) error {
	return dao.db.Create(activity).Error
}

// AddToWrongSet adds a question to the user's wrong set
func (dao *ActivityDAO) AddToWrongSet(wrongSet *models.WrongSet) error {
	return dao.db.Create(wrongSet).Error
}

// GetWrongSetByUser retrieves the wrong set for a specific user
func (dao *ActivityDAO) GetWrongSetByUser(userID uint) ([]models.WrongSet, error) {
	var wrongSet []models.WrongSet
	err := dao.db.Where("user_id = ?", userID).Find(&wrongSet).Error
	return wrongSet, err
}

// ResolveWrongQuestion resolves a wrong question for a user
func (dao *ActivityDAO) ResolveWrongQuestion(userID, questionID uint) error {
	return dao.db.Model(&models.WrongSet{}).
		Where("user_id = ? AND question_id = ?", userID, questionID).
		Delete(&models.WrongSet{}).Error
}

// UpdateQuestionStatus updates the status of a question for a user
func (dao *ActivityDAO) UpdateQuestionStatus(userID, questionID uint, status models.QuestionStatus) error {
	return dao.db.Model(&models.UserQuestionStatus{}).
		Where("user_id = ? AND question_id = ?", userID, questionID).
		Update("status", status).Error
}

// GetQuestionsByStatus retrieves questions by their status for a user
func (dao *ActivityDAO) GetQuestionsByStatus(userID uint, status models.QuestionStatus) ([]models.Question, error) {
	var questions []models.Question
	err := dao.db.Joins("JOIN user_question_statuses ON user_question_statuses.question_id = questions.id").
		Where("user_question_statuses.user_id = ? AND user_question_statuses.status = ?", userID, status).
		Find(&questions).Error
	return questions, err
}

// Add methods for ActivityDAO as needed
